/**
 * 全站权限配置
 *
 */
import router from './router/router';
import store from './store';
import {
  validatenull
} from '@/util/validate';
import {
  getToken
} from '@/util/auth';
import NProgress from 'nprogress'; //
import 'nprogress/nprogress.css';
import { getStore } from './util/store';
import Router from './router/router';
NProgress.configure({
  showSpinner: false
});
let flag = 0;
const lockPage = store.getters.website.lockPage; //锁屏页
function setMenu() {
  let menu = [
     {
       "parentId": "0",
       "moduleId": "5",
       "id": "5",
       "subSystemId": "0",
       "meta": {
         "keepAlive": false,
         "moduleid": "5",
         "i18n": ""
       },
       "target": "expand",
       "path": "/workflow",
       "label": "审批流程",
       "icon": "fa fa-share-alt",
       "component": "",
       "children": [
         {
           "parentId": "5",
           "moduleId": "S30HTk7t77",
           "id": "S30HTk7t77",
           "subSystemId": "0",
           "meta": {
             "keepAlive": false,
             "moduleid": "S30HTk7t77",
             "i18n": ""
           },
           "target": "iframe",
           "path": "/workflow/launch",
           "label": "流程发起",
           "icon": "fa fa-arrow-circle-right",
           "component": "views/WorkFlow/WFLaunch",
           "children": []
         }
       ]
     }
   ]
   store.commit("SET_MENUALL", menu);
   store.commit("SET_MENU", menu);
   router.$avueRouter.formatRoutes(menu, true);
 }
 let isSetMenu = false;
router.beforeEach((to, from, next) => {
  if(!isSetMenu) {
    setMenu();
  };
  if (to.path == '/wel/index' && from.path == '/') {
    store.commit('SET_TAG_LIST', []);
  }
  const meta = to.meta || {};
  const isMenu = meta.menu === undefined ? to.query.menu : meta.menu;
  store.commit('SET_IS_MENU', isMenu === undefined);
  if (getToken() || getStore({ name: 'token' })) {
    if (store.getters.isLock && to.path != lockPage) { //如果系统激活锁屏，全部跳转到锁屏页
      next({
        path: lockPage
      });
    } else if (to.path === '/login') { //如果登录成功访问登录页跳转到主页
      next({
        path: '/'
      });
    } else {
      const value = to.query.src || to.fullPath;
      const label = to.query.name || to.name;
      const meta = to.meta || router.$avueRouter.meta || {};
      const i18n = to.query.i18n;
      const target = to.query.target || router.$avueRouter.target || '';
      if (meta.isTab !== false && !validatenull(value) && !validatenull(label)) {
        store.commit('ADD_TAG', {
          label: label,
          value: value,
          params: to.params,
          query: to.query,
          target: target,
          meta: (() => {
            if (!i18n) {
              return meta;
            }
            return {
              i18n: i18n
            };
          })(),
          group: router.$avueRouter.group || []
        });
      }
      else if (flag === 0) {
        flag = 1;
        value.includes("preview.html")
      }
      next();
    }
  } else {
    if (meta.isAuth === false) {
      if (to.path !== '/login') {
        next({
          path: '/login'
        });
      } else {
        next();
      }
    } else {
      window.localStorage.setItem('loginBackPath', to.path);
      next('/login');
    }
  }
});

router.afterEach(() => {

  NProgress.done();
  let title = store.getters.tag.label;
  let i18n = store.getters.tag.meta.i18n;
  title = router.$avueRouter.generateTitle(title, i18n);
  router.$avueRouter.setTitle(title);

});